﻿using RndMaterialDisposalService.Attrbutes;
using RndMaterialDisposalService.Models;
using RndMaterialDisposalService.Models.Entitys;
using RndMaterialDisposalService.Models.RequestModel;
using RndMaterialDisposalService.Repository;
using RndMaterialDisposalService.Services.Interface;
using AutoMapper;
using Masuit.Tools;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Asn1.Cmp;

namespace RndMaterialDisposalService.Services
{
    /// <summary>
    /// 
    /// </summary>
    [NAutowired(Lifecycle.Scoped)]
    public class WeinxinUserService : IWeinxinUserService
    {
        
        private readonly EDPMiddleContext dbContext;
        private readonly IMapper mapper;

        /// <summary>
        /// 
        /// </summary>
        public Logger<WeinxinUserService> logger { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_dbContext"></param>
        /// <param name="_mapper"></param>
        public WeinxinUserService(EDPMiddleContext _dbContext, IMapper _mapper)
        {

            dbContext = _dbContext;
            mapper = _mapper;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<UserMiddleDto>> GetAddUserList()
        {
            //&& DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") == p.LastUpdateTime.ToString("yyyy-MM-dd")  && (DateTime.Now - p.LastUpdateTime).Days==1
            var userlist = await dbContext.UserData.Where(p => p.Action == 1 && p.Status != 2).ToListAsync();
            return userlist;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<UserMiddleDto>> GetDeleteUserIdList()
        {
            return await dbContext.UserData.Where(p => p.Action == 2 && p.Status != 2).ToListAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<UserMiddleDto>> GetModifyUserList()
        {
            var userlist = await dbContext.UserData.Where(p => p.Action == 3 && p.Status != 2).ToListAsync();
            return userlist;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public async Task<int> SyncUserToMiddleTable()
        {
            string sql = $@"EXEC [dbo].[sp_SynchUserToMiddleTable]";
            return await dbContext.Database.ExecuteSqlRawAsync(sql);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        public async Task<int> UpdateSyncStatus(List<UserMiddleDto> users)
        {
            users.ForEach(item => item.SyncTime = DateTime.Now);
            dbContext.UserData.UpdateRange(users);
            await dbContext.SaveChangesAsync();
            return users.Count;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="users"></param>
        /// <param name="actionType"></param>
        /// <returns></returns>
        public async Task<int> AddUserSyncLog(List<UserMiddleDto> users,string actionType)
        {
            var data = mapper.Map<List<WeixinUserSyncLogDto>>(users, opts => {
                opts.AfterMap((obj, list) => list.ForEach(item => item.ActionType = actionType));
            });
            dbContext.WeixinUserSyncLog.AddRange(data);
            return await dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> DeleteMiddleTableUser(List<UserMiddleDto> users)
        {
            dbContext.UserData.RemoveRange(users);
            await dbContext.SaveChangesAsync();
            return users.Count;
        }
    }
}
